function allNumber(v) {
    var reg = /^[0-9]*$/;
    if (reg.test(v)) {
        return true;
    }
    return false;
}

function CharMode(iN) {
    if (iN >= 48 && iN <= 57)//数字
        return 1;
    if (iN >= 65 && iN <= 90)//大写字母
        return 2;
    if (iN >= 97 && iN <= 122)//小写
        return 4;
    else
        return 8;//特殊字符
}

//计算出当前密码当中一共有多少种模式
function bitTotal(num) {
    var modes = 0;
    for (i = 0; i < 4; i++) {
        if (num & 1)
            modes++;
        num >>= 1;
    }
    return modes;
}

//返回密码的强度级别
function checkStrong(sPW) {
    if (sPW.length < 6)
        return 0;//密码太短
    var Modes = 0;
    for (i = 0; i < sPW.length; i++) {
        //测试每一个字符的类别并统计一共有多少种模式.
        Modes |= CharMode(sPW.charCodeAt(i));
    }
    //	alert(bitTotal(Modes));
    return bitTotal(Modes);
}


function showStrongPic() {
    var v = $("#pwd").val();
    var m = checkStrong(v);
    if (m < 2) {
        document.getElementById('lowPic').style.display = "";
        document.getElementById('midPic').style.display = "none";
        document.getElementById('highPic').style.display = "none";
    }
    else if (m == 2) {
        document.getElementById('lowPic').style.display = "none";
        document.getElementById('midPic').style.display = "";
        document.getElementById('highPic').style.display = "none";
    }
    else {
        document.getElementById('lowPic').style.display = "none";
        document.getElementById('midPic').style.display = "none";
        document.getElementById('highPic').style.display = "";
    }
}

//ajax用户验证
function isUserAccount() {
    var userAccount = document.getElementById('userAccount').value;
    var pattern = '^[0-9a-zA-z]{4,20}$';
    var regex = new RegExp(pattern);
    if ((userAccount == null) || (userAccount == "") || (userAccount.length < 4) || (userAccount.length > 20)) {
        document.getElementById('name_re').innerHTML = li_err;
        document.getElementById('name_re_m').innerHTML = '<span class=msg>用户名不能为空,请输入4-20个字符,只能是数字和字母!</span>';
        return false;
    }
    if(!userAccount.match(regex)){
        document.getElementById('name_re').innerHTML = li_err;
        document.getElementById('name_re_m').innerHTML = '<span class=msg>用户名只能是数字和字母组合!</span>';
        return false;
    }
    else {
        user_ajax(userAccount)
    }
}

function user_ajax(userAccount) {
    $.ajax({
        url:'/front/checkAccount',
        type:"post",
        data:{userAccount:userAccount},
        dataType:'json',
        contentType:'application/x-www-form-urlencoded',
        success:function (data) {
            console.log(data);
            if (data.code == 0) {
                document.getElementById('name_re').innerHTML = li_ok;
                document.getElementById('name_re_m').innerHTML = '<span class=msg2>可以注册！</span>';
                name_msg(0);
                return true;
            }else {
                document.getElementById('name_re').innerHTML = li_err;
                document.getElementById('name_re_m').innerHTML = '<span class=msg2>已存在账号！</span>';
                name_msg(0);
                return false;
            }
        },
        error:function () {

        }
    })
}



function name_msg(n) {
    var n = n;
    if (n == 0) {
        name_use = true;
    }
    else {
        name_use = false;
    }
}

//密码是否为空检测
function password1(){
    var pwd = $("#pwd").val();
    if (pwd == "" || pwd.length < 6) {
        document.getElementById('pwd_re').innerHTML = li_err;
        return false;
    }
    else {
        document.getElementById('pwd_re').innerHTML = li_ok;
        return true;
    }
}

//确认密码检测
function password2() {
    var pwd = document.getElementById('pwd').value;
    var pwd2 = document.getElementById('pwd2').value;
    if (pwd != pwd2) {
        document.getElementById('pwd2_re').innerHTML = li_err;
        document.getElementById('pwd2_re_m').innerHTML = '<span class=msg>两次密码不一致，请重新填写</span>';
        return false;
    }
    else {
        document.getElementById('pwd2_re').innerHTML = li_ok;
        document.getElementById('pwd2_re_m').innerHTML = '<span class=msg2>填写正确</span>';
        return true;
    }
}

//邮箱格式验证
function isEmail() {
    var email = document.getElementById('email').value;
    if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
        document.getElementById('email_re').innerHTML = li_ok;
        document.getElementById('email_re_m').innerHTML = '<span class=msg2>填写正确</span>';
        return true;
    }
    else {
        document.getElementById('email_re').innerHTML = li_err;
        document.getElementById('email_re_m').innerHTML = '<span class=msg>请填写正确的邮箱地址，格式为：123456@qq.com</span>';
        return false;
    }
}

//联系人
function isUserName() {
    var userName = document.getElementById('userName').value;
    if ((userName == null) || (userName == "")) {
        document.getElementById('userName_re').innerHTML = li_err;
        document.getElementById('userName_re_m').innerHTML = '<span class=msg>请填写用户姓名</span>';
        return false;
    }
    else {
        document.getElementById('userName_re').innerHTML = li_ok;
        document.getElementById('userName_re_m').innerHTML = '<span class=msg2>填写正确</span>';
        return true;
    }
}

//手机
function isMobile(){
    var mobile = $("#mobile").val();
    if ((mobile == null) || (mobile == "")) {
        document.getElementById('mobile_re').innerHTML = li_err;
        document.getElementById('mobile_re_m').innerHTML = '<span class=msg>请正确填写手机号码</span>';
        return false;
    }
    else {
        document.getElementById('mobile_re').innerHTML = li_ok;
        document.getElementById('mobile_re_m').innerHTML = '<span class=msg2>填写正确</span>';
        return true;
    }
}


//公司名
function isCompany() {
    var company = document.getElementById('company').value;
    if ((company == null) || (company == "")) {
        document.getElementById('company_re').innerHTML = li_err;
        document.getElementById('company_re_m').innerHTML = '<span class=msg>请正确填写公司名</span>';
        return false;
    }
    else {
        document.getElementById('company_re').innerHTML = li_ok;
        document.getElementById('company_re_m').innerHTML = '<span class=msg2>填写正确</span>';
        return true;
    }
}


//全表单提交验证
function tijiao() {
    if (isUserAccount() == false) {
        alert("用户账号不能为空,请输入4-20个字符！");
        return false;
    }
    if (isUserName() == false) {
        alert("用户名不正确，请重新填写！");
        return false;
    }
    if (isMobile() == false) {
        alert("手机号不正确，请重新填写！");
        return false;
    }
    if (password1() == false) {
        alert("密码必须填写！");
        return false;
    }
    if (password2() == false) {
        alert("确认密码错误！");
        return false;
    }
    if (isEmail() == false) {
        alert("邮箱地址为空或者错误！");
        return false;
    }
    if (isCompany() == false) {
        alert("请填写公司全称！");
        return false;
    }
}

//提交表单
function chk_reg() {
    if (tijiao() == false) {
        return false;
    }
    $.ajax({
        url:"/front/register",
        type:"post",
        data:$('#regform').serialize(),
        dataType:"json",
        success:function (data) {
            console.log(data);
            if (data.code == 0) {
                alert("注册成功,请登陆!");
                window.location.href = "/front/index";
            }
            else {
                alert(data.msg);
                return false;
            }
        },
        error:function (data) {
            console.log(data);
        }

    })
}

